package com.study.controller;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.study.dao.entity.MovieUser;
import com.study.service.MovieUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@RestController
public class MovieUserController {
	
	@Autowired
	private EurekaClient eurekaClient;
	
	@Autowired
	MovieUserService movieUserService;
	
//	@RequestMapping(value="/simple/{id}", method = RequestMethod.GET)
	@GetMapping("/simple/{id}")
	public MovieUser selectById(@PathVariable("id") int id) throws Exception {
		MovieUser movieUser = movieUserService.selectById(id);
//		Thread.sleep(3000);
		System.out.println("*******************************我是uesr2******************************");
		return movieUser;
	}
	
	@PostMapping("/postUser")
	public MovieUser postUser(@RequestBody MovieUser user) {
		System.out.println("user: " + user);
		return user;
	}
	
	

	/**
	 * 从Eureka服务器发现服务实例
	 */
	@GetMapping("/instance-info")
	public InstanceInfo serviceUrl() {
	    InstanceInfo instance = eurekaClient.getNextServerFromEureka("SUNXJ-MICROSERVICE-PROVIDER-USER", false);
	    return instance;
	}
	
	
	/**
	 * 使用RestTemplate时，想要获得一个List时，应该用数组，而不应该直接用List
	 */
	@GetMapping("/getAll")
	public List<MovieUser> getAll() {
		ArrayList<MovieUser> list = new ArrayList<>();
		MovieUser user1 = new MovieUser(1, "狗");
		MovieUser user2 = new MovieUser(2, "羊");
		list.add(user1);
		list.add(user2);
		return list;
	}

}
